// 火锅食材种植园游戏数据管理工具类

// 农场等级配置
export const FARM_LEVELS = [
    { level: 1, name: '小菜园', minExp: 0, maxPlots: 9, unlockSeeds: ['pepper', 'mushroom', 'vegetable'] },
    { level: 2, name: '中型农场', minExp: 100, maxPlots: 16, unlockSeeds: ['beef', 'lamb', 'shrimp'] },
    { level: 3, name: '大型农场', minExp: 300, maxPlots: 25, unlockSeeds: ['special_pepper', 'special_mushroom'] },
    { level: 4, name: '专业农场', minExp: 600, maxPlots: 36, unlockSeeds: ['premium_ingredients'] },
    { level: 5, name: '食材王国', minExp: 1000, maxPlots: 49, unlockSeeds: ['all'] }
]

// 种子配置
export const SEEDS = [
    {
        id: 'pepper',
        name: '辣椒',
        image: '/static/images/games/farm/pepper.png',
        price: 50,
        growthTime: 3600, // 1小时
        harvestValue: 100,
        category: 'spicy'
    },
    {
        id: 'mushroom',
        name: '蘑菇',
        image: '/static/images/games/farm/mushroom.png',
        price: 30,
        growthTime: 7200, // 2小时
        harvestValue: 80,
        category: 'vegetable'
    },
    {
        id: 'vegetable',
        name: '青菜',
        image: '/static/images/games/farm/vegetable.png',
        price: 20,
        growthTime: 1800, // 30分钟
        harvestValue: 50,
        category: 'vegetable'
    },
    {
        id: 'beef',
        name: '牛肉',
        image: '/static/images/games/farm/beef.png',
        price: 100,
        growthTime: 14400, // 4小时
        harvestValue: 200,
        category: 'meat'
    },
    {
        id: 'lamb',
        name: '羊肉',
        image: '/static/images/games/farm/lamb.png',
        price: 120,
        growthTime: 14400,
        harvestValue: 220,
        category: 'meat'
    },
    {
        id: 'shrimp',
        name: '虾滑',
        image: '/static/images/games/farm/shrimp.png',
        price: 80,
        growthTime: 10800, // 3小时
        harvestValue: 160,
        category: 'seafood'
    }
]

// 加工配方配置
export const RECIPES = [
    {
        id: 'spicy_soup',
        name: '麻辣底料',
        image: '/static/images/games/farm/spicy-soup.png',
        description: '使用辣椒制作麻辣底料',
        ingredients: ['pepper', 'special_pepper'],
        price: 200,
        quality: 1
    },
    {
        id: 'mushroom_soup',
        name: '菌菇底料',
        image: '/static/images/games/farm/mushroom-soup.png',
        description: '使用蘑菇制作菌菇底料',
        ingredients: ['mushroom', 'special_mushroom'],
        price: 150,
        quality: 1
    },
    {
        id: 'seafood_soup',
        name: '海鲜底料',
        image: '/static/images/games/farm/seafood-soup.png',
        description: '使用虾滑制作海鲜底料',
        ingredients: ['shrimp'],
        price: 180,
        quality: 1
    }
]

// 成就配置
export const ACHIEVEMENTS = [
    {
        id: 'beginner_farmer',
        name: '初级农夫',
        description: '种植10个食材',
        icon: '/static/images/games/farm/achievement1.png',
        requirement: 10,
        type: 'plant_count',
        reward: 100
    },
    {
        id: 'processing_master',
        name: '加工大师',
        description: '制作5种不同的底料',
        icon: '/static/images/games/farm/achievement2.png',
        requirement: 5,
        type: 'recipe_count',
        reward: 200
    },
    {
        id: 'social_butterfly',
        name: '社交达人',
        description: '与好友交换10次食材',
        icon: '/static/images/games/farm/achievement3.png',
        requirement: 10,
        type: 'exchange_count',
        reward: 150
    }
]

// 计算农场收入
export const calculateFarmIncome = (farmData) => {
    const baseIncome = 50
    const levelBonus = FARM_LEVELS.find(l => l.level === farmData.level).minExp / 100
    const plotBonus = farmData.plots.filter(plot => plot.plant && plot.plant.growthStage === 'mature').length
    
    return Math.floor(baseIncome * (1 + levelBonus) * (1 + plotBonus * 0.1))
}

// 检查是否可以升级农场
export const canUpgradeFarm = (farmData) => {
    const currentLevel = FARM_LEVELS.find(l => l.level === farmData.level)
    const nextLevel = FARM_LEVELS.find(l => l.level === farmData.level + 1)
    
    if (!nextLevel) return false
    
    return farmData.exp >= nextLevel.minExp
}

// 升级农场
export const upgradeFarm = (farmData) => {
    if (!canUpgradeFarm(farmData)) return false
    
    farmData.level++
    return true
}

// 检查是否可以解锁新地块
export const canUnlockPlot = (farmData) => {
    const currentLevel = FARM_LEVELS.find(l => l.level === farmData.level)
    return farmData.plots.length < currentLevel.maxPlots
}

// 解锁新地块
export const unlockPlot = (farmData) => {
    if (!canUnlockPlot(farmData)) return false
    
    farmData.plots.push({
        plant: null
    })
    return true
}

// 检查种子是否可用
export const isSeedAvailable = (farmData, seedId) => {
    const currentLevel = FARM_LEVELS.find(l => l.level === farmData.level)
    return currentLevel.unlockSeeds.includes('all') || currentLevel.unlockSeeds.includes(seedId)
}

// 种植种子
export const plantSeed = (farmData, plotIndex, seedId) => {
    const seed = SEEDS.find(s => s.id === seedId)
    if (!seed || !isSeedAvailable(farmData, seedId)) return false
    
    if (farmData.coins < seed.price) return false
    
    farmData.coins -= seed.price
    farmData.plots[plotIndex].plant = {
        ...seed,
        growthStage: 'seedling',
        timeLeft: seed.growthTime,
        plantTime: Date.now()
    }
    
    return true
}

// 收获作物
export const harvestPlant = (farmData, plotIndex) => {
    const plot = farmData.plots[plotIndex]
    if (!plot.plant || plot.plant.growthStage !== 'mature') return false
    
    farmData.coins += plot.plant.harvestValue
    farmData.exp += 10
    plot.plant = null
    
    return true
}

// 检查是否可以制作配方
export const canMakeRecipe = (farmData, recipeId) => {
    const recipe = RECIPES.find(r => r.id === recipeId)
    if (!recipe) return false
    
    // 检查食材是否足够
    for (const ingredientId of recipe.ingredients) {
        const hasIngredient = farmData.plots.some(plot => 
            plot.plant && plot.plant.id === ingredientId && plot.plant.growthStage === 'mature'
        )
        if (!hasIngredient) return false
    }
    
    return true
}

// 制作配方
export const makeRecipe = (farmData, recipeId) => {
    const recipe = RECIPES.find(r => r.id === recipeId)
    if (!recipe || !canMakeRecipe(farmData, recipeId)) return false
    
    // 消耗食材
    for (const ingredientId of recipe.ingredients) {
        const plot = farmData.plots.find(plot => 
            plot.plant && plot.plant.id === ingredientId && plot.plant.growthStage === 'mature'
        )
        if (plot) {
            plot.plant = null
        }
    }
    
    farmData.coins += recipe.price
    farmData.exp += 20
    
    return true
}

// 检查成就
export const checkAchievement = (farmData, achievementId) => {
    const achievement = ACHIEVEMENTS.find(a => a.id === achievementId)
    if (!achievement) return false
    
    let count = 0
    switch (achievement.type) {
        case 'plant_count':
            count = farmData.plots.filter(plot => plot.plant).length
            break
        case 'recipe_count':
            count = farmData.recipesMade?.length || 0
            break
        case 'exchange_count':
            count = farmData.exchangesMade?.length || 0
            break
    }
    
    return count >= achievement.requirement
}

// 解锁成就
export const unlockAchievement = (farmData, achievementId) => {
    const achievement = ACHIEVEMENTS.find(a => a.id === achievementId)
    if (!achievement || farmData.unlockedAchievements?.includes(achievementId)) return false
    
    if (!checkAchievement(farmData, achievementId)) return false
    
    farmData.coins += achievement.reward
    if (!farmData.unlockedAchievements) {
        farmData.unlockedAchievements = []
    }
    farmData.unlockedAchievements.push(achievementId)
    
    return true
}

// 保存游戏数据
export const saveGameData = (farmData) => {
    try {
        uni.setStorageSync('farmGameData', JSON.stringify(farmData))
        return true
    } catch (e) {
        console.error('保存游戏数据失败:', e)
        return false
    }
}

// 加载游戏数据
export const loadGameData = () => {
    try {
        const data = uni.getStorageSync('farmGameData')
        return data ? JSON.parse(data) : null
    } catch (e) {
        console.error('加载游戏数据失败:', e)
        return null
    }
} 